﻿module WorkBasket {
    export module FormTree {



        var _form: any;
        var scope: any;

        export function SetScope(form,ngScope) {
     
            scope = ngScope;
            _form = form;
        }
 


   
        export function addnode(ctlType,FormID) {


  
            var ref = $('#jstree').jstree(true), sel = ref.get_selected();
            var newnode = null;
            var selected = null;


            if (!sel.length) {
                selected = "#";
            } else {
                selected = sel[0];

               
            }
            var initvalue = {};  
            var DefaultProperties = JSON.parse(ctlType.Properties);

            for (var prop in DefaultProperties)
            {
                if (DefaultProperties.hasOwnProperty(prop))
                {
                    var vals = DefaultProperties[prop].split(",");
 
                    Object.defineProperty(initvalue, prop, {
                        value: vals[0],
                        writable: true,
                        configurable: true,
                        enumerable: true});
                }
            }

            var strValues = JSON.stringify(initvalue);
            var ParentID = selected == '#' ? 0 : selected;
            var fc = new WorkBasket.Entities.FormControl(
                0,
                scope.SelectedForm.FormID, 99,
                ctlType.ControlName,
                ctlType.FullName,
                strValues,
                ParentID);



            var obj: any = SOC.FormControls().SaveData(fc);
            var jsonnode = {
                "id": obj.FormControlID,
                "text": ctlType.ControlName,
                "icon": ctlType.ToolBoxIcon,
                "state": { "opened": true }
            };
           // ref.set_id(newnode, obj.FormControlID);

            newnode = ref.create_node(selected, jsonnode);
            

            return newnode;
        }


     

        function DeleteNode(data) {
            var obj: any;
            obj = WorkBasket.SOC.FormControls().GetByKey(data.id);
            WorkBasket.SOC.FormControls().Delete(obj, obj.FormControlID);

        }
        function loadproperties(data) {

            var ctl: any;
            ctl = WorkBasket.SOC.FormControls().GetByKey(data.node.id);
            var ctlType: any = SOC.Forms().GetByKey(ctl.ClassName.replace(/\./g, '_'), null, "GetControlData");
            var ctlDefaultProps = JSON.parse(ctlType.Properties);
            if (ctl != null) {
                var obj = JSON.parse(ctl.Properties);


                scope.Properties = [];
                for (var prop in obj) {
                    if (obj.hasOwnProperty(prop))
                        scope.Properties.push({ Name: prop, Value: obj[prop], Default: ctlDefaultProps[prop].split(",") });
                }
                scope.$digest();
            }
        }





        $(function () {



            $('#jstree').jstree(
             {
                    "core": {
                        "multiple": false,
                        "animation": 0,
                        "check_callback": true,
                        "themes": {
                            "variant": "large"
                        },

                        "data": function (obj, cb) {
                    
                            
                                if (_form != undefined) {
                                    var nodes = WorkBasket.SOC.FormControls().GetByParentKey(_form.FormID
                                        , "ControlsJsonFormat"
                                        , function (returndata) {


                                            for (var ix in returndata) {

                                                returndata[ix].li_attr = JSON.parse(returndata[ix].li_attr);
                                                returndata[ix].state = JSON.parse(returndata[ix].state);


                                            }
                                        return returndata;
                                    });
                                    cb.call(this, nodes);
                                } else {
                                    cb.call(this, []);
                                }
                            
                     },
                    },
                 "types": {
                     "control": {
                         "valid_children": ["control"]
                     }
                 },
                 'sort': function (a, b) {

                     var nodea = this.get_node(a);
                     var nodeb = this.get_node(b);
               
                     return nodea.li_attr.sort > nodeb.li_attr.sort ? 1 : -1;
                 },

                "contextmenu": {
                    "items": function ($node) {
                        var tree = $("#jstree").jstree(true);
                        return {
                            "Remove": {
                                "separator_before": false,
                                "separator_after": false,
                                "label": "Remove",
                                "action": function (obj) {
                                    tree.delete_node($node);
 
                                    DeleteNode($node);
                                }
                            }
                           
                        };
                    }
                },
                "plugins": ["themes", "dnd", "contextmenu","sort"]

               });



            $('#jstree').on("changed.jstree", function (e, data) {
                if (data.selected.length > 0) {
                    scope.SelectedControlID = data.selected[0];
                    loadproperties(data);
                }
            });
            $('#jstree').on("move_node.jstree.jstree", function (e, data) {

                
                var ctl: any;
                ctl = WorkBasket.SOC.FormControls().GetByKey(data.node.id);
                ctl.Sort = data.position; 
                ctl.ParentControlID = data.node.parent;
                WorkBasket.SOC.FormControls().SaveData(ctl);

            });

        });


    }
} 